# https://leetcode.cn/problems/koko-eating-bananas/description/
# 875. 爱吃香蕉的珂珂
# medium, 刘小康 2024.09.13
# 二分查找

class Solution:

    def minEatingSpeed(self, piles: list[int], h: int) -> int:
        left: int = 1
        right: int = 2147483647
        while left < right:     # 下界
            mid: int = left + ((right - left) >> 1)

            sum: int = 0
            for pile in piles:
                sum += self.eat_time(pile, mid)

            if sum <= h:
                right = mid
            else:
                left = mid + 1

        return left

    def eat_time(self, pile_count: int, eat_count: int) -> int:
        temp: int = pile_count // eat_count

        if temp * eat_count == pile_count:
            return temp
        # else
        return temp + 1
